package com.example.backend.controller;

import com.example.backend.common.ResultMessage;
import com.example.backend.entity.DingDan;
import com.example.backend.entity.XiangQing;
import com.example.backend.entity.vo.ShowCaiPin;
import com.example.backend.entity.vo.ShowYhDd;
import com.example.backend.service.IDingDanService;
import com.example.backend.service.IXiangQingService;
import com.example.backend.service.WorkService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/showYhDd")
@Log4j
public class ShowYhDdController {
    @Resource
    private WorkService workService;
    @Resource
    private IDingDanService dingDanService;
    @Resource
    private IXiangQingService xiangQingService;

    @PostMapping
    public ResultMessage addYhDd(@RequestBody ShowYhDd showYhDd){
        DingDan dingDan = new DingDan();
        dingDan.setYhid(showYhDd.getYhid());
        dingDan.setTPrice(showYhDd.getTotal());
        dingDan.setDdbz(showYhDd.getDdbz());
        dingDan.setDdzt(showYhDd.getDdzt());
        dingDan.setDddz(showYhDd.getDddz());
        dingDan.setDdsj(showYhDd.getDdsj());
        dingDan.setSjid(showYhDd.getSjid());
        boolean flag =  dingDanService.addDingDan(dingDan);
        if (flag){
            DingDan dingDan1 = dingDanService.queryDingDanByUserIdAndDate(showYhDd.getYhid(), String.valueOf(showYhDd.getDdsj()));
            XiangQing xiangQing = new XiangQing();
            xiangQing.setDdid(dingDan1.getDdid());
            for (ShowCaiPin showCaiPin : showYhDd.getCaiPin()) {
                xiangQing.setXqid(null);
                xiangQing.setCpid(showCaiPin.getCpid());
                xiangQing.setCpsl(showCaiPin.getCpsl());
                xiangQing.setCpdj(showCaiPin.getCpjg());
                xiangQingService.addXiangQing(xiangQing);
            }
            return ResultMessage.success(true,"添加成功");
        }
        return ResultMessage.error("添加失败");
    }

    @PostMapping("/query/{yhid}/{date}")
    public ResultMessage showYhDd(@PathVariable Integer yhid,@PathVariable String date){
        ShowYhDd showYhDd = workService.queryDdxqb(yhid,date);
        if (showYhDd != null){
            return ResultMessage.success(showYhDd, "查询成功");
        }
        return ResultMessage.error("查询失败");
    }


}
